let app = getApp();
var utils = require("../utils.js")

//内网穿透工具介绍:
// https://open-doc.dingtalk.com/microapp/debug/ucof2g
//替换成开发者后台设置的安全域名
let domain = "http://localhost:8080";
let url = domain + '/login';

Page({
    data:{
        corpId: '',
        authCode:'',
        userId:'',
        userName:'',
        hideList: true,
        userInfo: {},
        location: {
          ready: false
        },
        location_error: ""
    },
    getUserInfo() {
      let that = this;
      var authCode = null;
      dd.getAuthCode({
        success:function(res){
          console.log("授权码", res);
          authCode = res.authCode;
          // dd.alert(res.authCode)
          let promise = utils.getUserInfo(authCode);
          promise.then((userinfo)=>{
            app.globalData.userInfo = userinfo;
            console.log("userinfo: ", app.globalData.userInfo);
            that.setData({
              userInfo: userinfo
            })
          }, (err)=>{
            dd.alert("获取用户信息失败"+JSON.stringify(err))
          })
        },
        fail:function(err){
          dd.alert("获取授权码失败"+JSON.stringify(err))
        }
      });
      
    },
    clear() {
      this.setData({
        location: {}
      })
    },
    updateVersion() {
      if(app.globalData.updateManager){
        dd.alert("暂无更新");
        console.log("至下次冷启动前不再检查更新-index");
        return
      }
      dd.showLoading({ 
        content: '正在获取新版本中...',
      });
      let updateManager=dd.getUpdateManager();
      updateManager.onCheckForUpdate(function (res) { 
        console.log("是否有更新", res.hasUpdate) // 是否有更新
        if (res.hasUpdate) { 
          updateManager.onUpdateReady(function (ret) { 
            console.log("更新版本号:", ret.version) // 更新版本号
            dd.hideLoading()
            dd.confirm({ 
              title: '更新提示',
              content: '新版本已经准备好，是否重启应用？',
              success: function (res) { 
                if (res.confirm) { 
                  updateManager.applyUpdate()
                }
              }
            })
          })
        } else { 
          dd.hideLoading()
          dd.alert({ 
            title: '已经是新版本了！',
            buttonText: '我知道了',
            success: () => { },
          })
        }
        app.globalData.updateManager = true;
        dd.hideLoading()
      })
      updateManager.onUpdateFailed(function () { 
        dd.hideLoading()
        // 新版本下载失败
        dd.alert({ 
          title: '新版本下载失败！请稍后重试！',
          buttonText: '我知道了',
          success: () => { },
        })
      })
    },
    loginSystem() {
        // dd.showLoading();
        let that = this;
        dd.getLocation({
          success(resp) {
            console.log("location:", resp);
            that.setData({
              location: {
                ready: true,
                longitude: resp.longitude,
                latitude: resp.latitude,
                accuracy: resp.accuracy,
                province: resp.province,
                city: resp.city,
                address: resp.address
              }
            })
          },
          fail(err) {
            console.log("定位失败", JSON.stringify(err));
            that.setData({
              location: {
                location_error: JSON.stringify(err)
              }
            })
            dd.alert({ title: '定位失败, 请检查是否打开定位功能' });
          },
        });

    },
    onLoad(){

        let _this = this;

        this.setData({
            corpId: app.globalData.corpId
        })
        
        //dd.alert({content: "step1"});
         
        
        
    }
})